package thread.synchronized_;

public class Demo02_SynMethod {
    public static void main(String[] args) {
        Printer2 printer2 = new Printer2();


        new Thread() {
            int count = 500;
            @Override
            public void run() {
                while (count-- >0) {
                    printer2.print1();
                }

            }
        }.start();

        new Thread() {
            int count = 500;
            @Override
            public void run() {
                while (count-->0) {
                    printer2.print2();
                }

            }
        }.start();

    }
}
/*

 * 非静态同步函数的锁是:this
 * 静态的同步函数的锁是:字节码对象
 */

class Printer2 {
    Demo demo = new Demo();
    //非静态的同步方法的锁对象是什么？
    //answer：非静态的同步方法的锁对象是：this


    synchronized void print1() {
        //同步代码块，锁对象可以是任意的,锁对象不能用匿名对象，
        //匿名对象不是同一个对象
        System.out.print("1");
        System.out.print("2");
        System.out.print("3");
        System.out.print("4");
        System.out.print("5");
        System.out.print("6");
        System.out.print("7");
        System.out.print("8");
        System.out.print("9");
        System.out.println("\r\n");

    }

    void print2() {
        synchronized (this) {
            System.out.print("t1");
            System.out.print("t2");
            System.out.print("t3");
            System.out.print("t4");
            System.out.print("t5");
            System.out.print("t6");
            System.out.print("t7");
            System.out.print("t8");
            System.out.print("t9");
            System.out.println("\r\n");

        }
    }

}
